import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:fixnum/fixnum.dart' as $fixnum;

import '../../../../lib_basemodel/user/user.dart';
import '../../../../resource/language/generated/l10n.dart';
import '../../interface/root_proxy_model.dart';
import 'doufeng_tacit_model.dart';
import 'inviting_from_widget.dart';
import 'inviting_refuse_widget.dart';
import 'inviting_to_widget.dart';
import 'question_answer_widget.dart';
import 'stop_tacit_widget.dart';
import 'tacit_end_widget.dart';
import 'waiting_start_widget.dart';

typedef CountDownCallback = Widget Function(int seconds);

class DoufengTacitDialogManager {
  /// 快问快答
  static showQuestionAnswerDialog(
      BuildContext context, /*TacitPush tacitPush, */ String? names) {
    showDialogWithoutTopBar(
      context,
      routeSettingsName: names,
      MultiProvider(
        providers: [
          ChangeNotifierProxyProvider<RootProxyModel, DoufengTacitModel>(
            create: (context) => DoufengTacitModel(
              context: context,
              autoDismissSec: 10,
              // tacitPush: tacitPush,
              onAutoDismissCallback: () {},
            ),
            update: (context, rootProxyModel, leafModel) {
              if (leafModel == null) throw ArgumentError.notNull('leafModel');
              leafModel.roomModel = rootProxyModel;
              return leafModel;
            },
          ),
        ],
        child: QuestionAnswerWidget(),
      ),
      barrierDismissible: true,
    );
  }

  static showInvitingRefuseDialog(
      BuildContext context /** ,TacitPush tacitPush */) {
    showDialogWithoutTopBar(
      context,
      MultiProvider(
        providers: [
          ChangeNotifierProxyProvider<RootProxyModel, DoufengTacitModel>(
            create: (context) => DoufengTacitModel(
              context: context,
              autoDismissSec: 10,
              // tacitPush: tacitPush,
              onAutoDismissCallback: () {
                Navigator.pop(context);
              },
            ),
            update: (context, rootProxyModel, leafModel) {
              if (leafModel == null) throw ArgumentError.notNull('leafModel');
              leafModel.roomModel = rootProxyModel;
              return leafModel;
            },
          ),
        ],
        child: InvitingRefuseWidget(),
      ),
      barrierDismissible: true,
    );
  }

  static showInvitingFromDialog(
      BuildContext context, User userFrom, User userTo) {
    showDialogWithoutTopBar(
      context,
      MultiProvider(
          providers: [
            ChangeNotifierProxyProvider<RootProxyModel, DoufengTacitModel>(
              create: (context) => DoufengTacitModel(
                  context: context,
                  autoDismissSec: 10,
                  onAutoDismissCallback: () {}),
              update: (context, rootProxyModel, leafModel) {
                if (leafModel == null) throw ArgumentError.notNull('leafModel');
                leafModel.roomModel = rootProxyModel;
                return leafModel;
              },
            ),
          ],
          child: InvitingFromWidget(
            userFrom: userFrom,
            userTo: userTo,
          )),
      barrierDismissible: true,
    );
  }

  static showStopTacitDialog(BuildContext context, User user,
      VoidCallback onConfirm, VoidCallback onCancel) {
    showDialogWithoutTopBar(
      context,
      MultiProvider(providers: [
        ChangeNotifierProxyProvider<RootProxyModel, DoufengTacitModel>(
          create: (context) => DoufengTacitModel(
              context: context,
              autoDismissSec: 10,
              onAutoDismissCallback: () {}),
          update: (context, rootProxyModel, leafModel) {
            if (leafModel == null) throw ArgumentError.notNull('leafModel');
            leafModel.roomModel = rootProxyModel;
            return leafModel;
          },
        ),
      ], child: StopTacitWidget()),
      barrierDismissible: true,
    );
  }

  static showWaitingStartDialog(BuildContext context) {
    showDialogWithoutTopBar(
      context,
      MultiProvider(providers: [
        ChangeNotifierProxyProvider<RootProxyModel, DoufengTacitModel>(
          create: (context) => DoufengTacitModel(
              context: context,
              autoDismissSec: 10,
              onAutoDismissCallback: () {}),
          update: (context, rootProxyModel, leafModel) {
            if (leafModel == null) throw ArgumentError.notNull('leafModel');
            leafModel.roomModel = rootProxyModel;
            return leafModel;
          },
        ),
      ], child: WaitingStartWidget()),
      //   MultiProvider(
      //       providers: [
      //         ChangeNotifierProvider(create: (context) => DoufengTacitModel(
      //             context: context,
      //             autoDismissSec: 10,
      //             onAutoDismissCallback: () {}))],
      //       child: WaitingStartWidget()),
      barrierDismissible: true,
    );
  }

  static showInvitingGsDialog(BuildContext context,
      /* TacitPush tacitPush, */ $fixnum.Int64 channelId) {
    showDialogWithoutTopBar(
      context,
      MultiProvider(
        providers: [
          ChangeNotifierProxyProvider<RootProxyModel, DoufengTacitModel>(
            create: (context) => DoufengTacitModel(
              context: context,
              autoDismissSec: 10,
              popAfterAutoDismiss: false,
              onAutoDismissCallback: () {
                DoufengTacitModel.tacitResponse(context, true, channelId);
              },
            ),
            update: (context, rootProxyModel, leafModel) {
              if (leafModel == null) throw ArgumentError.notNull('leafModel');
              leafModel.roomModel = rootProxyModel;
              return leafModel;
            },
          ),
        ],
        child: InvitingToWidget(),
        // child: Container(
        //   width: 100,
        //   height: 100,
        //   color: Colors.red,
        // ),
      ),
      barrierDismissible: true,
    );
  }

  /// 弹出开启默契测试
  static showInfoDialog(BuildContext context, VoidCallback onConfirm) {
    showDialogWithTopBar(
      context,
      topImageStr: "ic_doufeng_tacit_top",
      Container(
        padding: const EdgeInsets.only(top: 50, left: 34, right: 34),
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            const Text(
              "开启默契测验，通过快问快答来测试你们之间的默契值，总共10题，快来测一测你与他的默契值怎么样吧~",
              style: TextStyle(
                fontSize: 13,
                fontWeight: FontWeight.w500,
                color: Colors.white,
              ),
            ),
            CupertinoButton(
              onPressed: () {
                onConfirm();
                Navigator.pop(context);
              },
              minSize: 0,
              padding: const EdgeInsets.only(bottom: 20),
              child: Container(
                width: 128,
                height: 42,
                decoration: BoxDecoration(
                  gradient: const LinearGradient(
                      colors: [Color(0xFFEC6FA9), Color(0xFFEA3B7B)]),
                  borderRadius: BorderRadius.circular(21),
                ),
                alignment: Alignment.center,
                child: Text(
                  T.current.open_now,
                  style: const TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w500,
                    color: Colors.white,
                  ),
                ),
              ),
            )
          ],
        ),
      ),
      barrierDismissible: true,
    );
  }

  static showDialogWithEndTopBar(BuildContext context, Widget child,
      {bool barrierDismissible = false, VoidCallback? onAutoDismissCallback}) {
    showDialog(
        context: context,
        barrierDismissible: barrierDismissible,
        useRootNavigator: false,
        builder: (_) {
          return Dialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(16.0)),
              backgroundColor: Colors.transparent,
              insetPadding: EdgeInsets.zero,
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(16),
                ),
                width: double.infinity,
                height: 407,
                child: Stack(
                  children: [
                    Align(
                      alignment: Alignment.bottomCenter,
                      child: Container(
                        margin: const EdgeInsets.only(
                            left: 16, right: 16, top: 30, bottom: 24),
                        width: double.infinity,
                        height: 238,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(16),
                            border: Border.all(
                                color: const Color(0xFFFF99A0), width: 10),
                            color: const Color(0xFF4B3593)),
                        child: child,
                      ),
                    ),
                    // Container(
                    //   margin: EdgeInsets.symmetric(horizontal: 16),
                    //   width: double.infinity,
                    //   height: 238,
                    //   decoration: BoxDecoration(
                    //     // borderRadius: BorderRadius.circular(16),
                    //     // border: Border.all(color: Color(0xFFFF99A0),width: 10),
                    //       color: Colors.white
                    //   ),
                    //   child: child,
                    // )
                    Align(
                      alignment: Alignment.topCenter,
                      child: Image.asset(
                        "assets/images/ic_doufeng_tacit_end_top.png",
                        width: 158,
                        height: 70,
                        fit: BoxFit.contain,
                      ),
                    ),
                  ],
                ),
              ));
        });
  }

  static showDialogWithoutTopBar(BuildContext context, Widget child,
      {bool barrierDismissible = false, String? routeSettingsName}) {
    showDialog(
      context: context,
      barrierDismissible: barrierDismissible,
      useRootNavigator: false,
      routeSettings: RouteSettings(name: routeSettingsName),
      builder: (_) {
        return Dialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16.0),
          ),
          backgroundColor: Colors.transparent,
          insetPadding: EdgeInsets.zero,
          child: Container(
            margin: const EdgeInsets.only(left: 16, right: 16, top: 47),
            width: double.infinity,
            height: 238,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(16),
              border: Border.all(color: const Color(0xFFFF99A0), width: 10),
              color: const Color(0xFF4B3593),
            ),
            child: child,
          ),
        );
      },
    );
  }

  /// 默契测试结束
  static showEndDialog<T extends RootProxyModel>(
      BuildContext context, /* TacitPush tacitPush, */ String? names) {
    showDialogWithTopBar(
      context,
      height: 380,
      topImageStr: "ic_doufeng_tacit_end_top",
      MultiProvider(
        providers: [
          ChangeNotifierProxyProvider<T, DoufengTacitModel>(
            create: (context) => DoufengTacitModel(
              context: context,
              autoDismissSec: 10,
              // tacitPush: tacitPush,
              onAutoDismissCallback: () {},
            ),
            update: (context, rootProxyModel, leafModel) {
              if (leafModel == null) throw ArgumentError.notNull('leafModel');
              leafModel.roomModel = rootProxyModel;
              return leafModel;
            },
          ),
        ],
        child: TacitEndWidget(),
      ),
      barrierDismissible: true,
    );
  }

  /// 带顶部图片的自定义组件
  static showDialogWithTopBar(
    BuildContext context,
    Widget child, {
    int autoDismissSec = 0,
    bool barrierDismissible = false,
    VoidCallback? onAutoDismissCallback,
    String? topImageStr,
    double height = 285,
  }) {
    showDialog(
      context: context,
      barrierDismissible: barrierDismissible,
      useRootNavigator: false,
      builder: (_) {
        return Dialog(
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(16.0)),
          backgroundColor: Colors.transparent,
          insetPadding: EdgeInsets.zero,
          child: Expanded(
            child: Container(
              alignment: Alignment.bottomCenter,
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(16),
                ),
                width: double.infinity,
                height: height,
                child: Stack(
                  children: [
                    Container(
                      margin:
                          const EdgeInsets.only(left: 16, right: 16, top: 47),
                      width: double.infinity,
                      height: height,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(16),
                        border: Border.all(
                          color: const Color(0xFFFF99A0),
                          width: 10,
                        ),
                        color: const Color(0xFF4B3593),
                      ),
                      child: child,
                    ),
                    Align(
                      alignment: Alignment.topCenter,
                      child: Image.asset(
                        "assets/images/$topImageStr.png",
                        width: 158,
                        height: 70,
                        fit: BoxFit.contain,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
    if (autoDismissSec > 0) {
      Future.delayed(
        Duration(seconds: autoDismissSec),
        () {
          onAutoDismissCallback?.call();
          Navigator.pop(context);
        },
      );
    }
  }
}
